﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Museum.DBAccess;
using System.Configuration;
using System.IO;
using System.Data;

public partial class TourDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                ddlMuseum.DataSource = DBAccess.GetMuseumByCity("");
                ddlMuseum.DataBind();

                if (Session["MuseumId"] != null)
                {
                    if (Convert.ToInt32(Session["MuseumId"]) > 0)
                    {
                        ddlMuseum.SelectedValue = Session["MuseumId"].ToString();
                        ddlMuseum.Enabled = false;
                    }
                }

                BindTourData();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Binds the control data.
    /// </summary>
    private void BindTourData()
    {
        DataTable dtTour = null;
        DataTable dtArtAssets = null;
        int artID = 0;
        try
        {
            if (Request.QueryString["ArtID"] != null)
            {
                artID = Convert.ToInt32(Request.QueryString["ArtID"]);
                dtTour = DBAccess.GetTourById(artID);

                dtArtAssets = DBAccess.GetArtAssetsByArtId(artID);
            }
            if (dtArtAssets != null && dtArtAssets.Rows.Count > 0)
            {
                string[] assetIds = new string[7];
                string[] filePaths = new string[7];

                for (int i = 0; i < dtArtAssets.Rows.Count; )
                {
                    if (dtArtAssets.Rows[i]["IsImage"].ToString().Equals("True"))
                    {
                        assetIds[i] = dtArtAssets.Rows[i]["ArtAssetID"].ToString();
                        filePaths[i] = dtArtAssets.Rows[i]["FilePath"].ToString();
                    }
                    else if (dtArtAssets.Rows[i]["IsAudio"].ToString().Equals("True") && i >= 5)
                    {
                        assetIds[i] = dtArtAssets.Rows[i]["ArtAssetID"].ToString();
                        filePaths[i] = dtArtAssets.Rows[i]["FilePath"].ToString();
                    }
                    else if (dtArtAssets.Rows[i]["IsVideo"].ToString().Equals("True") && i > 5)
                    {
                        assetIds[i] = dtArtAssets.Rows[i]["ArtAssetID"].ToString();
                        filePaths[i] = dtArtAssets.Rows[i]["FilePath"].ToString();
                    }
                    else
                    {
                        DataRow row = dtArtAssets.NewRow();
                        row["ArtAssetID"] = 0;
                        row["ArtId"] = 0;
                        row["IsImage"] = false;
                        row["IsAudio"] = false;
                        row["IsVideo"] = false;
                        row["FilePath"] = "";
                        row["CreatedDate"] = DateTime.Now;
                        dtArtAssets.Rows.InsertAt(row, i);

                        assetIds[i] = null;
                        filePaths[i] = "";
                    }
                    i++;
                }

                if (assetIds[0] != null)
                {
                    lblArtAsset1.Text = assetIds[0].ToString();
                    lblImage1.Text = filePaths[0].ToString();
                    lblImage1.ToolTip = filePaths[0].ToString();
                    lblImage1.Visible = true;
                    lnkbtnDeleteImage1.Visible = true;
                }
                else
                {
                    lblImage1.Visible = false;
                    lnkbtnDeleteImage1.Visible = false;
                }

                if (assetIds[1] != null)
                {
                    lblArtAsset2.Text = assetIds[1].ToString();
                    lblImage2.Text = filePaths[1].ToString();
                    lblImage2.ToolTip = filePaths[1].ToString();
                    lblImage2.Visible = true;
                    lnkbtnDeleteImage2.Visible = true;
                }
                else
                {
                    lblImage2.Visible = false;
                    lnkbtnDeleteImage2.Visible = false;
                }

                if (assetIds[2] != null)
                {
                    lblArtAsset3.Text = assetIds[2].ToString();
                    lblImage3.Text = filePaths[2].ToString();
                    lblImage3.ToolTip = filePaths[2].ToString();
                    lblImage3.Visible = true;
                    lnkbtnDeleteImage3.Visible = true;
                }
                else
                {
                    lblImage3.Visible = false;
                    lnkbtnDeleteImage3.Visible = false;
                }

                if (assetIds[3] != null)
                {
                    lblArtAsset4.Text = assetIds[3].ToString();
                    lblImage4.Text = filePaths[3].ToString();
                    lblImage4.ToolTip = filePaths[3].ToString();
                    lblImage4.Visible = true;
                    lnkbtnDeleteImage4.Visible = true;
                }
                else
                {
                    lblImage4.Visible = false;
                    lnkbtnDeleteImage4.Visible = false;
                }

                if (assetIds[4] != null)
                {
                    lblArtAsset5.Text = assetIds[4].ToString();
                    lblImage5.Text = filePaths[4].ToString();
                    lblImage5.ToolTip = filePaths[4].ToString();
                    lblImage5.Visible = true;
                    lnkbtnDeleteImage5.Visible = true;
                }
                else
                {
                    lblImage5.Visible = false;
                    lnkbtnDeleteImage5.Visible = false;
                }

                if (assetIds[5] != null)
                {
                    lblArtAsset6.Text = assetIds[5].ToString();
                    lblImage6.Text = filePaths[5].ToString();
                    lblImage6.ToolTip = filePaths[5].ToString();
                    lblImage6.Visible = true;
                    lnkbtnDeleteAudio.Visible = true;
                }
                else
                {
                    lblImage6.Visible = false;
                    lnkbtnDeleteAudio.Visible = false;
                }

                if (assetIds[6] != null)
                {
                    lblArtAsset7.Text = assetIds[6].ToString();
                    lblImage7.Text = filePaths[6].ToString();
                    lblImage7.ToolTip = filePaths[6].ToString();
                    lblImage7.Visible = true;
                    lnkbtnDeleteVideo.Visible = true;
                }
                else
                {
                    lblImage7.Visible = false;
                    lnkbtnDeleteVideo.Visible = false;
                }
            }
            else if (dtTour != null && dtTour.Rows.Count > 0)
            {
                lblImage1.Text = dtTour.Rows[0]["ArtImagePath"].ToString();
                lblImage2.Visible = false;
                lblImage3.Visible = false;
                lblImage4.Visible = false;
                lblImage5.Visible = false;
                lblImage6.Visible = false;
                lblImage7.Visible = false;
                lnkbtnDeleteImage2.Visible = false;
                lnkbtnDeleteImage3.Visible = false;
                lnkbtnDeleteImage4.Visible = false;
                lnkbtnDeleteImage5.Visible = false;
                lnkbtnDeleteAudio.Visible = false;
                lnkbtnDeleteVideo.Visible = false;
            }

            ddlCategory.DataSource = DBAccess.GetCategories(Convert.ToInt32(ddlMuseum.SelectedValue));
            ddlCategory.DataBind();

            if (dtTour != null && dtTour.Rows.Count > 0)
            {
                txtArtName.Text = dtTour.Rows[0]["ArtName"].ToString();
                txtArtistName.Text = dtTour.Rows[0]["ArtistName"].ToString();
                txtArtDescription.Text = dtTour.Rows[0]["ArtDescription"].ToString();
                txtFloor.Text = dtTour.Rows[0]["FloorNumber"].ToString();
                txtGallery.Text = dtTour.Rows[0]["Gallery"].ToString();
                txtStop.Text = dtTour.Rows[0]["StopNumber"].ToString();
                ddlMuseum.SelectedValue = dtTour.Rows[0]["MuseumId"].ToString();
                ddlCategory.SelectedValue = dtTour.Rows[0]["CategoryId"].ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected string[] UploadImages()
    {
        string[] URLs = null;
        URLs = new string[5];

        if (fileUploadArtImage1.HasFile)
        {
            byte[] thumbnailData = null;

            thumbnailData = new byte[fileUploadArtImage1.FileBytes.Length];
            thumbnailData = fileUploadArtImage1.FileBytes;

            //Create a new subfolder under the current active folder
            string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

            // Create the subfolder
            if (!Directory.Exists(newPath))
                System.IO.Directory.CreateDirectory(newPath);

            // Create the subfolder
            if (!Directory.Exists(newPath + "/1"))
                System.IO.Directory.CreateDirectory(newPath + "/1");

            // Combine the new file name with the path
            newPath = System.IO.Path.Combine(newPath + "/1", fileUploadArtImage1.FileName);

            FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
            fs.Write(thumbnailData, 0, thumbnailData.Length);
            fs.Close();

            URLs[0] = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/1/" + fileUploadArtImage1.FileName;
        }
        if (fileUploadArtImage2.HasFile)
        {
            string thumbnailURL = string.Empty;
            byte[] thumbnailData = null;

            thumbnailData = new byte[fileUploadArtImage2.FileBytes.Length];
            thumbnailData = fileUploadArtImage2.FileBytes;

            //Create a new subfolder under the current active folder
            string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

            // Create the subfolder
            if (!Directory.Exists(newPath))
                System.IO.Directory.CreateDirectory(newPath);

            // Create the subfolder
            if (!Directory.Exists(newPath + "/2"))
                System.IO.Directory.CreateDirectory(newPath + "/2");

            // Combine the new file name with the path
            newPath = System.IO.Path.Combine(newPath + "/2", fileUploadArtImage2.FileName);

            FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
            fs.Write(thumbnailData, 0, thumbnailData.Length);
            fs.Close();

            URLs[1] = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/2/" + fileUploadArtImage2.FileName;
        }
        if (fileUploadArtImage3.HasFile)
        {
            string thumbnailURL = string.Empty;
            byte[] thumbnailData = null;

            thumbnailData = new byte[fileUploadArtImage3.FileBytes.Length];
            thumbnailData = fileUploadArtImage3.FileBytes;

            //Create a new subfolder under the current active folder
            string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

            // Create the subfolder
            if (!Directory.Exists(newPath))
                System.IO.Directory.CreateDirectory(newPath);

            // Create the subfolder
            if (!Directory.Exists(newPath + "/3"))
                System.IO.Directory.CreateDirectory(newPath + "/3");

            // Combine the new file name with the path
            newPath = System.IO.Path.Combine(newPath + "/3", fileUploadArtImage3.FileName);

            FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
            fs.Write(thumbnailData, 0, thumbnailData.Length);
            fs.Close();

            URLs[2] = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/3/" + fileUploadArtImage3.FileName;
        }
        if (fileUploadArtImage4.HasFile)
        {
            string thumbnailURL = string.Empty;
            byte[] thumbnailData = null;

            thumbnailData = new byte[fileUploadArtImage4.FileBytes.Length];
            thumbnailData = fileUploadArtImage4.FileBytes;

            //Create a new subfolder under the current active folder
            string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

            // Create the subfolder
            if (!Directory.Exists(newPath))
                System.IO.Directory.CreateDirectory(newPath);

            // Create the subfolder
            if (!Directory.Exists(newPath + "/4"))
                System.IO.Directory.CreateDirectory(newPath + "/4");

            // Combine the new file name with the path
            newPath = System.IO.Path.Combine(newPath + "/4", fileUploadArtImage4.FileName);

            FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
            fs.Write(thumbnailData, 0, thumbnailData.Length);
            fs.Close();

            URLs[3] = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/4/" + fileUploadArtImage4.FileName;
        }
        if (fileUploadArtImage5.HasFile)
        {
            string thumbnailURL = string.Empty;
            byte[] thumbnailData = null;

            thumbnailData = new byte[fileUploadArtImage5.FileBytes.Length];
            thumbnailData = fileUploadArtImage5.FileBytes;

            //Create a new subfolder under the current active folder
            string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

            // Create the subfolder
            if (!Directory.Exists(newPath))
                System.IO.Directory.CreateDirectory(newPath);

            // Create the subfolder
            if (!Directory.Exists(newPath + "/5"))
                System.IO.Directory.CreateDirectory(newPath + "/5");

            // Combine the new file name with the path
            newPath = System.IO.Path.Combine(newPath + "/5", fileUploadArtImage5.FileName);

            FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
            fs.Write(thumbnailData, 0, thumbnailData.Length);
            fs.Close();

            URLs[4] = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/5/" + fileUploadArtImage5.FileName;
        }
        return URLs;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string[] ImageURLs = null;
        byte[] thumbnailAudioData = null;
        byte[] thumbnailVideoData = null;
        string thumbnailAudioURL = string.Empty;
        string thumbnailVideoURL = string.Empty;
        int artID = 0;
        try
        {
            if (Request.QueryString["ArtID"] != null)
                artID = Convert.ToInt32(Request.QueryString["ArtID"]);

            ImageURLs = new string[5];
            ImageURLs = UploadImages();

            if (fileUploadAudio.HasFile)
            {
                thumbnailAudioData = new byte[fileUploadAudio.FileBytes.Length];
                thumbnailAudioData = fileUploadAudio.FileBytes;

                //Create a new subfolder under the current active folder
                string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

                // Create the subfolder
                if (!Directory.Exists(newPath))
                    System.IO.Directory.CreateDirectory(newPath);

                // Combine the new file name with the path
                newPath = System.IO.Path.Combine(newPath, fileUploadAudio.FileName);

                FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
                fs.Write(thumbnailAudioData, 0, thumbnailAudioData.Length);
                fs.Close();

                thumbnailAudioURL = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/" + fileUploadAudio.FileName;
            }

            if (fileUploadVideo.HasFile)
            {
                thumbnailVideoData = new byte[fileUploadVideo.FileBytes.Length];
                thumbnailVideoData = fileUploadVideo.FileBytes;

                //Create a new subfolder under the current active folder
                string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Tours/", txtArtName.Text.Trim().Replace("'", ""));

                // Create the subfolder
                if (!Directory.Exists(newPath))
                    System.IO.Directory.CreateDirectory(newPath);

                // Combine the new file name with the path
                newPath = System.IO.Path.Combine(newPath, fileUploadVideo.FileName);

                FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write);
                fs.Write(thumbnailVideoData, 0, thumbnailVideoData.Length);
                fs.Close();

                thumbnailVideoURL = ConfigurationManager.AppSettings["ServerURL"] + "Tours/" + txtArtName.Text.Trim().Replace("'", "") + "/" + fileUploadVideo.FileName;
            }

            DBAccess.InsertOrUpdateTourDetails(Convert.ToInt32(ddlMuseum.SelectedItem.Value), Convert.ToInt32(ddlCategory.SelectedItem.Value), artID
            , txtArtName.Text.Trim(), txtArtistName.Text.Trim(), txtArtDescription.Text.Trim()
            , txtStop.Text.Trim(), txtFloor.Text.Trim(), txtGallery.Text.Trim(), "", "", "", ImageURLs[0], ImageURLs[1], ImageURLs[2], ImageURLs[3], ImageURLs[4]
            , thumbnailAudioURL, thumbnailVideoURL, ImageURLs[0], Convert.ToInt32(lblArtAsset1.Text.Trim()), Convert.ToInt32(lblArtAsset2.Text.Trim())
            , Convert.ToInt32(lblArtAsset3.Text.Trim()), Convert.ToInt32(lblArtAsset4.Text.Trim()), Convert.ToInt32(lblArtAsset5.Text.Trim())
            , Convert.ToInt32(lblArtAsset6.Text.Trim()), Convert.ToInt32(lblArtAsset7.Text.Trim()));

            if (artID > 0)
            {
                Alert.ShowMessage("Tour updated successfully.");
                Response.Redirect("ManageTours.aspx", false);
            }
            else
            {
                Alert.ShowMessage("Tour inserted successfully.");
                Response.Redirect("ManageTours.aspx", false);
            }
            ClearControls();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ClearControls();
    }
    private void ClearControls()
    {
        txtArtName.Text = "";
        txtArtistName.Text = "";
        txtArtDescription.Text = "";
        txtFloor.Text = "";
        txtGallery.Text = "";
        txtStop.Text = "";
        lblImage1.Text = "";
        lblImage2.Text = "";
        lblImage3.Text = "";
        lblImage4.Text = "";
        lblImage5.Text = "";
        lblImage6.Text = "";
        lblImage7.Text = "";
        lblImage1.Visible = false;
        lblImage2.Visible = false;
        lblImage3.Visible = false;
        lblImage4.Visible = false;
        lblImage5.Visible = false;
        lblImage6.Visible = false;
        lblImage7.Visible = false;
        lnkbtnDeleteImage1.Visible = false;
        lnkbtnDeleteImage2.Visible = false;
        lnkbtnDeleteImage3.Visible = false;
        lnkbtnDeleteImage4.Visible = false;
        lnkbtnDeleteImage5.Visible = false;
        lnkbtnDeleteAudio.Visible = false;
        lnkbtnDeleteVideo.Visible = false;
    }

    protected void ddlMuseum_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlCategory.DataSource = DBAccess.GetCategories(Convert.ToInt32(ddlMuseum.SelectedValue));
        ddlCategory.DataBind();
    }

    protected void lnkbtnDeleteImage1_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset1.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
    protected void lnkbtnDeleteImage2_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset2.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
    protected void lnkbtnDeleteImage3_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset3.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
    protected void lnkbtnDeleteImage4_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset4.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
    protected void lnkbtnDeleteImage5_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset5.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
    protected void lnkbtnDeleteAudio_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset6.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
    protected void lnkbtnDeleteVideo_Click(object sender, EventArgs e)
    {
        try
        {
            int result = DBAccess.DeleteArtAssetData(Convert.ToInt32(lblArtAsset7.Text.Trim()));

            if (result.Equals(-1))
            {
                Alert.ShowMessage("Tour data deleted successfully.");
                BindTourData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
}